//
//  YCForgetPasswordEnsureViewController.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/9.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  忘记密码确定

#import "YCForgetPasswordEnsureViewController.h"
#import "YCLoginViewController.h"

@interface YCForgetPasswordEnsureViewController ()<UITextFieldDelegate>

/** scrollView  */
@property (nonatomic, strong) UIScrollView *scrollView;
/** contentView  */
@property (nonatomic, strong) UIView *contentView;

/// 忘记密码标题
@property (nonatomic,readonly) UILabel *forgetPwdLabel;
/// 输入新密码
@property (nonatomic,readonly) UITextField *newPasswordField;
/// 确认新密码
@property (nonatomic,readonly) UITextField *ensureNewPasswordField;
/// 确定按钮
@property (nonatomic,readonly) UIButton *ensureButton;


@end

@implementation YCForgetPasswordEnsureViewController

@synthesize forgetPwdLabel = _forgetPwdLabel;
@synthesize newPasswordField = _newPasswordField;
@synthesize ensureNewPasswordField = _ensureNewPasswordField;
@synthesize ensureButton = _ensureButton;

#pragma mark - 懒加载
// 忘记密码
- (UILabel *)forgetPwdLabel
{
    if(!_forgetPwdLabel)
    {
        _forgetPwdLabel = [[UILabel alloc] init];
        _forgetPwdLabel.text = @"忘记密码";
        _forgetPwdLabel.textColor = [UIColor yc_colorWithUInt:0x3d3e40];
        _forgetPwdLabel.textAlignment = NSTextAlignmentCenter;
        _forgetPwdLabel.font = [UIFont systemFontOfSize:24];
    }
    return _forgetPwdLabel;
}

// 输入新密码
- (UITextField *)newPasswordField
{
    if(!_newPasswordField)
    {
        _newPasswordField = [[UITextField alloc] init];
        _newPasswordField.layer.borderWidth = 1;
        _newPasswordField.layer.borderColor = [UIColor yc_colorWithUInt:0xe0e0e0].CGColor;
        _newPasswordField.layer.cornerRadius= 17.5;
        _newPasswordField.layer.masksToBounds = YES;
        _newPasswordField.font = [UIFont systemFontOfSize:15];
        _newPasswordField.backgroundColor = [UIColor whiteColor];
        _newPasswordField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _newPasswordField.placeholder = NSLocalizedString(@"请输入新密码", nil);//有默认颜色
        _newPasswordField.returnKeyType = UIReturnKeyNext;
        _newPasswordField.keyboardType = UIKeyboardTypeNumberPad;
        _newPasswordField.textColor = [UIColor yc_colorWithUInt:0x3d3e40];
        _newPasswordField.delegate = self;
        
        
        //leftView
        UILabel *tipLabel = [[UILabel alloc] init];
        tipLabel.font = [UIFont boldSystemFontOfSize:15];
        CGSize tipLabelSize = [@"输入新密码" yc_sizeWithFont:tipLabel.font MaxSize:CGSizeMake(CGFLOAT_MAX, 0)];
        tipLabel.frame = CGRectMake(0, 0, tipLabelSize.width + 34, 40);
        tipLabel.textColor = [UIColor yc_colorWithUInt:0x3d3e40];
        tipLabel.text = NSLocalizedString(@"输入新密码", nil);
        tipLabel.textAlignment = NSTextAlignmentCenter;
        _newPasswordField.leftView = tipLabel;
        _newPasswordField.leftViewMode = UITextFieldViewModeAlways;
    }
    return _newPasswordField;
}

// 确认新密码
- (UITextField *)ensureNewPasswordField
{
    if(!_ensureNewPasswordField)
    {
        _ensureNewPasswordField = [[UITextField alloc] init];
        _ensureNewPasswordField.layer.borderWidth = 1;
        _ensureNewPasswordField.layer.borderColor = [UIColor yc_colorWithUInt:0xe0e0e0].CGColor;
        _ensureNewPasswordField.layer.cornerRadius= 17.5;
        _ensureNewPasswordField.layer.masksToBounds = YES;
        _ensureNewPasswordField.font = [UIFont systemFontOfSize:15];
        _ensureNewPasswordField.backgroundColor = [UIColor whiteColor];
        _ensureNewPasswordField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _ensureNewPasswordField.placeholder = NSLocalizedString(@"再次输入新密码", nil);//有默认颜色
        _ensureNewPasswordField.returnKeyType = UIReturnKeyNext;
        _ensureNewPasswordField.keyboardType = UIKeyboardTypeNumberPad;
        _ensureNewPasswordField.textColor = [UIColor yc_colorWithUInt:0x3d3e40];
        _ensureNewPasswordField.delegate = self;
        
        
        //leftView
        UILabel *tipLabel = [[UILabel alloc] init];
        tipLabel.font = [UIFont boldSystemFontOfSize:15];
        CGSize tipLabelSize = [@"确认新密码" yc_sizeWithFont:tipLabel.font MaxSize:CGSizeMake(CGFLOAT_MAX, 0)];
        tipLabel.frame = CGRectMake(0, 0, tipLabelSize.width + 34, 40);
        tipLabel.textColor = [UIColor yc_colorWithUInt:0x3d3e40];
        tipLabel.text = NSLocalizedString(@"确认新密码", nil);
        tipLabel.textAlignment = NSTextAlignmentCenter;
        _ensureNewPasswordField.leftView = tipLabel;
        _ensureNewPasswordField.leftViewMode = UITextFieldViewModeAlways;
    }
    return _ensureNewPasswordField;
}

- (UIButton *)ensureButton
{
    if(!_ensureButton)
    {
        _ensureButton = [[UIButton alloc] init];
        _ensureButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [_ensureButton setTitleColor:[UIColor yc_colorWithUInt:0x3d3e40] forState:UIControlStateNormal];
        _ensureButton.backgroundColor = [UIColor yc_colorWithUInt:0xf8bb36];
        _ensureButton.layer.cornerRadius = 17.5;
        [_ensureButton setTitle:NSLocalizedString(@"确定", nil) forState:UIControlStateNormal];
        [_ensureButton addTarget:self action:@selector(didTouchUpInsideEnsureButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _ensureButton;
}

#pragma mark - 生命周期

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self makeView];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveKeyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)makeView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.alwaysBounceVertical = YES;
    //    self.scrollView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    self.scrollView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    // 添加单击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapScrollView:)];
    [self.scrollView addGestureRecognizer:tap];
    [self.view addSubview:self.scrollView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor yc_colorWithUInt:0xf7f7f7];
    self.contentView = contentView;
    [self.scrollView addSubview:contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    
    [self.contentView addSubview:self.forgetPwdLabel];
    [self.contentView addSubview:self.newPasswordField];
    [self.contentView addSubview:self.ensureNewPasswordField];
    [self.contentView addSubview:self.ensureButton];
    
    [_forgetPwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        [YCAdapter performOtherBlock:^{
            make.top.equalTo(self.contentView).offset(150);
        } IPhone6Block:^{
            make.top.equalTo(self.contentView).offset(206);
        } IPhone6pBlock:^{
            make.top.equalTo(self.contentView).offset(220);
        }];
        
        make.centerX.equalTo(self.contentView);
        make.height.equalTo(@48);
    }];
    
    [_newPasswordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_forgetPwdLabel.mas_bottom).with.offset(heightFromIphone6Height(60));
        make.centerX.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(47);
        make.right.equalTo(self.contentView).offset(-47);
        make.height.equalTo(@40);
    }];
    
    [_ensureNewPasswordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_newPasswordField.mas_bottom).with.offset(heightFromIphone6Height(20));
        make.centerX.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(47);
        make.right.equalTo(self.contentView).offset(-47);
        make.height.equalTo(@40);
    }];
    
    
    [_ensureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_ensureNewPasswordField.mas_bottom).with.offset(heightFromIphone6Height(83));
        make.centerX.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(47);
        make.right.equalTo(self.contentView).offset(-47);
        make.height.equalTo(@36);
    }];
    
    
    [self.contentView layoutIfNeeded];
    CGFloat contentH = CGRectGetMaxY(_ensureButton.frame);
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.bottom.equalTo(_logoImageView.mas_bottom).offset(heightFromIphone6Height(63));
        make.bottom.equalTo(_ensureButton.mas_bottom).offset(kScreenHeight - contentH+64);
    }];
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.contentView.frame)-64);
}


#pragma mark -- 响应事件
///确定按钮
- (void)didTouchUpInsideEnsureButton:(UIButton *)button
{
    YCLog(@"确定");
    [self.view endEditing:YES];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 回到登录页
        [self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:YES];
    });
    
    [SVProgressHUD showWithStatus:@"确定..."];
    
    [SVProgressHUD dismissWithDelay:1];
    
}


///点击回弹键盘
- (void)tapScrollView:(UITapGestureRecognizer *)tap
{
    YCLogFunc
    [self.view endEditing:YES];
}

#pragma mark - UITextField delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField == self.newPasswordField)
    {
        [self.ensureNewPasswordField becomeFirstResponder];
    }
    else
    {
        [self.ensureButton sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
    return YES;
}

#pragma mark - 通知回调

///键盘将要弹出通知回调
- (void)receiveKeyboardWillChangeFrameNotification:(NSNotification *)notification
{
    CGRect rect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //    if(CGRectIsEmpty(_loginButtonInitFrame))
    //    {
    //        _loginButtonInitFrame = [self.view convertRect:self.loginButton.frame toView:self.view.window];
    //    }
    //    if(rect.origin.y < GO_IPHONE_HEIGHT)
    //    {
    //        CGFloat offset = CGRectGetMaxY(_loginButtonInitFrame) - CGRectGetMinY(rect);
    //        if(offset < 0 ) return;
    //        rect = self.view.bounds;
    //        rect.origin.y = (offset);
    //        [UIView animateWithDuration:duration animations:^{
    //            self.view.bounds = rect;
    //        }];
    //    }
    //    else
    //    {
    //        [UIView animateWithDuration:duration animations:^{
    //            self.view.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
    //        }];
    //    }
}






@end
